/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-02-06     HZHJYYJQR       the first version
 */
#ifndef APPLICATIONS_HJCOMM_HJRFCARD_CPP_
#define APPLICATIONS_HJCOMM_HJRFCARD_CPP_


#include "HJRfCard.h"
#include "bsp/bsp_uart2.h"
#include "bsp/bsp_converter.h"

#define BYTE_ORDER               true
#define BYTE_REVERSE_ORDER       false

//组包 包 -> 数组
void RFCard::make_pack(uint8_t* buff, uint16_t* len)
{
    uint16_t index = 0;
    uint8_t xor_verify= 0;
    buff[index++] = 0xAA;
    buff[index++] = 0xBB;

    bsp_cnv_uint16_to_arr(buff + index, this->data_len + 5, BYTE_REVERSE_ORDER);
    index += 2;

    bsp_cnv_uint16_to_arr(buff + index, this->device, BYTE_ORDER);
    index += 2;

    bsp_cnv_uint16_to_arr(buff + index, this->cmd, BYTE_ORDER);
    index += 2;

    buff[index++]=0x00;   //status

    memcpy(buff + index, this->data, this->data_len);
    index += this->data_len;

    xor_verify = xor_sum(buff + 4, index - 4);
    buff[index++] = xor_verify;

    *len = index;
}
//
//解包 数组 -> 包
void RFCard::unmake_pack(uint8_t* buff, uint16_t* len)
{
    uint16_t index = 0;
    this->head1 = buff[index++];
    this->head2 = buff[index++];

    this->len = bsp_cnv_arr_to_uint16(buff + index, BYTE_REVERSE_ORDER);
    index += 2;

    this->device = bsp_cnv_arr_to_uint16(buff + index, BYTE_ORDER);
    index += 2;

    this->cmd = bsp_cnv_arr_to_uint16(buff + index, BYTE_ORDER);
    index += 2;

    this->data_len = *len - 9;
    memcpy(this->data, buff + index, this->data_len);
    index += this->data_len;

    this->sum = buff[index++];
}


uint8_t RFCard::xor_sum(uint8_t* buff, uint16_t len)
{
    uint32_t xor_init = 0x00;

    for(uint16_t i=0;i<len;i++)xor_init=xor_init^buff[i];

    return xor_init;
}

bool RFCardRecv::proc_one_byte(RFCard* rfcard,uint8_t data)
{
//    rt_kprintf("%d \r\n",data);
    if (recv_len == 0)
    {
        recv_buff[0] = recv_buff[1];
        recv_buff[1] = recv_buff[2];
        recv_buff[2] = recv_buff[3];
        recv_buff[3] = data;

        uint16_t len = bsp_cnv_arr_to_uint16(&recv_buff[2], BYTE_REVERSE_ORDER);
        if (recv_buff[0] == 0xAA && recv_buff[1] == 0xBB &&
            len <= 1024 && len > 4)
        {
            recv_index = 4;
            recv_len = len + 4;
            //qDebug("接收到帧头");
        }
    }
    else
    {
        if(data==0xaa)recv_len ++;
        recv_buff[recv_index++] = data;
        if (recv_index >= recv_len)
        {
                uint8_t xor_verify = rfcard->xor_sum(recv_buff + 4, recv_len - 5);
                if (xor_verify == recv_buff[recv_len-1])//异或校验
                {
//                    rt_kprintf("NFC RECV:");
//                    for(uint8_t i=0;i<recv_len;i++)rt_kprintf("%02x ",recv_buff[i]);
//                    rt_kprintf("\r\n");
                    rfcard->unmake_pack(recv_buff, &recv_len); //解包
                    recv_index = 0;
                    recv_len = 0;
                    return true;
                }
                else
                {
                    rt_kprintf("crc error...\r\n");
//                    qDebug("crc校验失败");
                }
                recv_index = 0;
                recv_len = 0;
        }
    }
    return false;
}


#endif /* APPLICATIONS_HJCOMM_HJRFCARD_CPP_ */
